﻿using System;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.FrameOrder.Dto;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using NP.BPMReportPlatform.Entities.BPM_Business;
using System.Linq;
using Microsoft.AspNetCore.Http;
using System.IO;

namespace NP.BPMReportPlatform.FrameOrder
{
    // [Authorize]
    [ApiController]
    [Route("api/frameOrder")]
    public class FrameOrderController : BPMReportPlatformController
    {
        private readonly IFrameOrderService _frameOrderService;


        public FrameOrderController(IFrameOrderService frameOrderService)
        {
            _frameOrderService = frameOrderService;

        }

        /// <summary>
        ///车辆购置 交通费用查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/GetAllAsync")]
        public async Task<PagedResultDto<GetFrameOrderOutput>> GetAllAsync(
            [FromBody] GetFrameOrderInput input)
        {
            return await _frameOrderService.GetAllAsync(input);
        }

        /// <summary>
        /// 交通费用报销 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/JTFYBX/ExportAsync")]
        public async Task<XlsxFileResult<GetJTFrameOrderOutput>> JTFYBXExportAsync(
            [FromBody] GetFrameOrderInput input)
        {

            // ExporterHeaderInfo
            var rst = await _frameOrderService.ExportAsync(input);
            // var result = await _excelExporter.Export("导出Excel", rst);


            var result = new List<GetJTFrameOrderOutput>();


            return new XlsxFileResult<GetJTFrameOrderOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }


        /// <summary>
        /// 车辆购置补贴 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/CLGZBT/ExportAsync")]
        public async Task<XlsxFileResult<GetCLFrameOrderOutput>> CLGZBTExportAsync(
            [FromBody] GetFrameOrderInput input)
        {

            // ExporterHeaderInfo
            var rst = await _frameOrderService.ExportAsync(input);
            // var result = await _excelExporter.Export("导出Excel", rst);

            var result = new List<GetCLFrameOrderOutput>();


            return new XlsxFileResult<GetCLFrameOrderOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }

        /// <summary>
        /// 驻外补贴 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/RSZWBT/ExportAsync")]
        public async Task<XlsxFileResult<GetRSZWBTFrameOrderOutput>> CLRSZWBTExportAsync(
            [FromBody] GetFrameOrderInput input)
        {
            // ExporterHeaderInfo
            var rst = await _frameOrderService.ExportAsync(input);

            var result = new List<GetRSZWBTFrameOrderOutput>();


            return new XlsxFileResult<GetRSZWBTFrameOrderOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }

        /// <summary>
        ///  更新对应流程编号的数据为薪资组确认状态
        /// </summary>
        /// <param name="instanceNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("FrameOrder/ConfirmSalary/{instanceNumber}")]
        public async Task ConfirmSalary([FromRoute] string instanceNumber)
        {
            await _frameOrderService.UpdateBusinessType(instanceNumber);
        }

        /// <summary>
        /// 薪资组批量确认
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/BatchConfirmationAsync")]
        public async Task BatchConfirmation([FromBody] List<string> input)
        {
            await _frameOrderService.BatchConfirmation(input);
        }

        /// <summary>
        /// 获取框架订单推送记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/GetFraneOrderPushRecordAsync")]
        public async Task<PagedResultDto<GetFraneOrderPushRecordOutput>> GetFraneOrderPushRecordAsync([FromBody] GetFraneOrderPushRecordInput input)
        {
            return await _frameOrderService.GetFraneOrderPushRecordAsync(input);
        }

        /// <summary>
        /// 导出框架订单推送记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/ExportFraneOrderPushRecordAsync")]
        public async Task<XlsxFileResult<GetFraneOrderPushRecordOutput>> ExportFraneOrderPushRecordAsync([FromBody] GetFraneOrderPushRecordInput input)
        {
            var rst = await _frameOrderService.ExportFraneOrderPushRecordAsync(input);
            return new XlsxFileResult<GetFraneOrderPushRecordOutput>(rst, "导出Excel");
        }

        /// <summary>
        /// 推送选中的数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/SendThirdSystem")]
        public async Task SendThirdSystem([FromBody] IEnumerable<Guid> input)
        {
            await _frameOrderService.SendThirdSystem(input);
        }

        #region 探亲次数推送报表

        /// <summary>
        ///探亲次数 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/GetVisitAsync")]
        public async Task<PagedResultDto<FrameOrderChildrszwbt>> GetVisitAsync(
          [FromBody] GetFrameOrderInput input)
        {
            return await _frameOrderService.GetVisitAsync(input);
        }

        /// <summary>
        /// 探亲次数重新推送
        /// </summary>
        /// <param name="instanceNumber">流程编号</param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/Visit/PushRetry")]
        public async Task<string> PushVisitRetryAsync(string instanceNumber)
        {
            return await _frameOrderService.PushVisitRetryAsync(instanceNumber);
        }

        /// <summary>
        /// 探亲次数 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/Visit/ExportAsync")]
        public async Task<XlsxFileResult<FrameOrderChildrszwbtOutput>> VisitExportAsync(
            [FromBody] GetFrameOrderInput input)
        {
            var rst = await _frameOrderService.GetVisitAsync(input);
            var result = new List<FrameOrderChildrszwbtOutput>();
            return new XlsxFileResult<FrameOrderChildrszwbtOutput>(ObjectMapper.Map(rst.Items.ToList(), result), "导出Excel");
        }

        /// <summary>
        /// 驻外补贴 重新推送
        /// </summary>
        /// <param name="instanceNumber">流程编号</param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/Visit/PushRetryToSF")]
        public async Task<string> PushRetryToSF(string instanceNumber)
        {
            return await _frameOrderService.PushRSZWToSF(instanceNumber);
        }

        #endregion

        #region 租房津贴查询报表

        /// <summary>
        ///租房津贴 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/GetRentalAllowanceAsync")]
        public async Task<PagedResultDto<GetFrameOrderPullingRecordOutput>> GetRentalAllowanceAsync(
          [FromBody] GetFraneOrderPushRecordInput input)
        {
            return await _frameOrderService.GetRentalAllowanceAsync(input);
        }

        /// <summary>
        /// 租房津贴 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/RentalAllowance/ExportAsync")]
        public async Task<XlsxFileResult<GetFrameOrderPullingRecordOutput>> RentalAllowanceExportAsync(
            [FromBody] GetFraneOrderPushRecordInput input)
        {
            input.PageSize = Int32.MaxValue;
            var rst = await _frameOrderService.GetRentalAllowanceAsync(input);
            return new XlsxFileResult<GetFrameOrderPullingRecordOutput>(rst.Items.ToList(), "导出Excel");
        }

        /// <summary>
        /// 导入框架补贴数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FrameOrder/ImporterExcelAsync")]
        public async Task<ImportResultExt<FrameOrderImportDto.ImportInput>> ImporterExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<FrameOrderImportDto.ImportInput>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            return await _frameOrderService.ImporterExcelAsync(bytes);
        }

        private byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
        #endregion
    }
}